﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace University.Workshop.WinApp.Hospitalization
{
    public partial class SelectBedForm : Form
    {
        private HospitalizationForm hospitalizationForm;
        private List<Model.Bed> beds;
        private string selectedBed;
        private Button selectedButton;

        public SelectBedForm(HospitalizationForm hospitalizationForm)
        {
            InitializeComponent();
            this.hospitalizationForm = hospitalizationForm;
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(selectedBed))
            {
                hospitalizationForm.SetSelectedBed(selectedBed);
                this.Close();
            }
            else
                MessageBox.Show("Elija una cama");
        }

        private void SelectBedForm_Load(object sender, EventArgs e)
        {
            beds = (List<Model.Bed>) Business.HospitalizationManager.ObtainAllBeds();
            LoadBeds();
        }

        private void ChangeBedToOccupied(Button button)
        {
            button.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.BedRed));
            button.BackColor = Color.Red;
        }
        private void LoadBeds()
        {
            foreach (var bed in beds)
            {
                if (bed.State.Equals("Ocupado"))
                {
                    switch(bed.BedId)
                    {
                        case "101-1": ChangeBedToOccupied(btn1011); break;
                        case "101-2": ChangeBedToOccupied(btn1012); break;
                        case "101-3": ChangeBedToOccupied(btn1013); break;
                        case "102-1": ChangeBedToOccupied(btn1021); break;
                        case "102-2": ChangeBedToOccupied(btn1022); break;
                        case "102-3": ChangeBedToOccupied(btn1023); break;
                        case "103-1": ChangeBedToOccupied(btn1031); break;
                        case "103-2": ChangeBedToOccupied(btn1032); break;
                        case "103-3": ChangeBedToOccupied(btn1033); break;
                        case "201-1": ChangeBedToOccupied(btn2011); break;
                        case "201-2": ChangeBedToOccupied(btn2012); break;
                        case "202-1": ChangeBedToOccupied(btn2021); break;
                        case "202-2": ChangeBedToOccupied(btn2022); break;
                        case "203-1": ChangeBedToOccupied(btn2031); break;
                        case "203-2": ChangeBedToOccupied(btn2032); break;
                        case "301-1": ChangeBedToOccupied(btn3011); break;
                        case "302-1": ChangeBedToOccupied(btn3021); break;
                        case "303-1": ChangeBedToOccupied(btn3031); break;
                        default: break;
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ChangeBedState(Button newSelection, string bedId)
        {
            if (newSelection.BackColor == Color.White)
            {
                if (selectedButton != null)
                {
                    selectedButton.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Bed));
                    selectedButton.BackColor = Color.White;
                }
                selectedBed = bedId;
                newSelection.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.BedGreen));
                newSelection.BackColor = Color.Green;
                selectedButton = newSelection;
            }
        }

        private void btn1011_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn1011,"101-1");
        }

        private void btn1012_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn1012, "101-2");
        }

        private void btn1013_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn1013, "101-3");
        }

        private void btn1021_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn1021, "102-1");
        }

        private void btn1022_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn1022, "102-1");
        }

        private void btn1023_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn1013, "102-3");
        }

        private void btn1031_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn1031, "103-1");
        }

        private void btn1032_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn1032, "103-2");
        }

        private void btn1033_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn1033, "103-3");
        }

        private void btn2011_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn2011, "201-1");
        }

        private void btn2012_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn2012, "201-2");
        }

        private void btn2021_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn2021, "202-1");
        }

        private void btn2022_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn2022, "202-2");
        }

        private void btn2031_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn2031, "203-1");
        }

        private void btn2032_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn2032, "203-2");
        }

        private void btn3011_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn3011, "301-1");
        }

        private void btn3021_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn3021, "302-1");
        }

        private void btn3031_Click(object sender, EventArgs e)
        {
            ChangeBedState(btn3031, "303-3");
        }



    }
}
